<?php

namespace Home\Model;

use Think\Model;

class UserModel extends Model {

	//protected $insertFields = array('UE_account','UE_accName','nickname','email');

	//protected $updateFields = array('nickname','email');

	protected $_validate = array(
	

		//	array('UE_account','/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/','用户名格式不对！',1),

			array('UE_email','UE_email_repeat','用户名两次输入不对！',1,'confirm',self::MODEL_BOTH),

			array('UE_account','','用户名已存在！',1,'unique',self::MODEL_BOTH),

		//	array('UE_accName','/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/','介绍人用户名格式不对！',1),

			array('UE_accName','require','介绍人必填！',1),
			
		//	array('answer','密保答案必填！',1),

			array('UE_accName','validate','介绍人不存在！',1,'callback',self::MODEL_BOTH),

		//	array('UE_accName','UE_accName1','介绍人两次输入不对！',1,'confirm',self::MODEL_BOTH),

			array('UE_theme','1,50','昵称1-10个汉字！',1,'length',self::MODEL_BOTH),

			
		//	array('UE_theme','require','昵称必填！',1),

			//array('UE_theme','validate','代理中心不存在！',1,'callback',self::MODEL_BOTH),		

		//	array('UE_qq','require','QQ必填！',1),

			array('UE_phone','require','手机必填！',1),
			array('UE_phone','','手机已存在！',1,'unique',self::MODEL_BOTH),
			//array('email','require','邮箱必填！',1),

		//	array('UE_password','/^[a-zA-Z0-9]{6,15}$/','登入密码6-12个字元,大小写英文+数字,请勿用特殊词符！',1),

		//	array('UE_secpwd','/^[a-zA-Z0-9]{6,15}$/','二级密码6-12个字元,大小写英文+数字,请勿用特殊词符！',1),

			array('UE_password','UE_repwd','两次输入密码不同！',1,'confirm',self::MODEL_BOTH),
			array('UE_secpwd','UE_resecpwd','两次输入二级密码不同！',1,'confirm',self::MODEL_BOTH),

			array('pin','/^[a-zA-Z0-9]{1,60}$/','PIN码输入不对,或已使用！',1),

			array('pin','pinyz','PIN码输入不对,或已使用！',1,'callback',self::MODEL_BOTH),

			//array('pin','pin2','两次输入PIN码不同！',1,'confirm',self::MODEL_BOTH),

	);

	protected $_auto = array (

			array('UE_password','md5',3,'function') ,

			array('UE_secpwd','md5',3,'function')

	);

	public function validate($data){

		if(M('user')->where(array('UE_account'=>$data))->find()){

			return TRUE;

		}else{

			return FALSE;

		}

	}

	
	public function pinyz($data){

		if(M('pin')->where(array('pin'=>$data,'zt'=>'0'))->find()){

			return TRUE;

		}else{

			return FALSE;

		}

	}

	public function validate1($data){

		if(M('user')->where(array('UE_account'=>$data))->find()){

			return TRUE;

		}else{

			return FALSE;

		}

	}

	
	/**
	* 计算打款时间间隔 
	* ppdd_id  int  ot_ppdd表中ID
	*/
	public function get_time_interval($ppdd_id = 0){
        
		/*查询数据*/
		$ppdd=M('ppdd')->where(array('id'=>$ppdd_id,'zt'=>'2'))->find();
		
		// echo "<pre>";
		// print_r($ppdd);
		// echo "</pre>";
		
		if(!empty($ppdd['date']) || !empty($ppdd['date_hk'])){
			
			$date = strtotime($ppdd['date']); //开始时间
			$date_hk = strtotime($ppdd['date_hk']); //打款时间
			
			// echo "开始时间".$date."<br/>";
			// echo "打款时间".$date_hk."<br/>";
			
			/*相差时间*/
			$hour = 0;
			$hour = $date_hk-$date;
			
			/*奖励比例*/
			$reward = 0;
			if($hour >= 0 && $hour <= 21600){
				$reward = C("jjdkwhattimejl1")*0.01;	
			}elseif($hour > 21600 && $hour <= 64800){
				$reward = C("jjdkwhattimejl2")*0.01;
			}elseif($hour > 64800 && $hour <= 86400){
				$reward = C("jjdkwhattimejl3")*0.01;
			}
			
			// echo "相差小时数".$hour."<br/>";
			// echo "奖励比例".$reward."<br/>";
			
			/*奖励金额*/
			$reward_money = $ppdd['jb']*$reward;
			
			// echo "金额".$ppdd['jb']."<br/>";
			// echo "奖励金额".$reward_money."<br/>";
			
			/*更新用户账户余额*/
			if($reward_money > 0){
				
				$user = M('user')->where(array(UE_account => $ppdd['p_user']))->find();
				$UE_money = $user['ue_money']*1+$reward_money;
				
				M('user')->where(array('UE_account' => $ppdd['p_user']))->save(array('UE_money' => $UE_money));
				
				/*并写入账户资金变动记录*/
				$data2['user'] = $ppdd['p_user'];
				$data2['r_id'] = $ppdd['id'];
				$data2['date'] = date('Y-m-d H:i:s', time()); //操作時間
				$data2['note'] = '打款奖励';
				$data2['jb'] = $reward_money;
				$data2['jj'] = $reward_money;
				M('user_jl')->add($data2);
				
			}
			
			return $reward_money;

		}

	}
	
	
	
	
	
	
	
	
}